//
//  ViewLoanViewController.m
//  Wallets Manager
//
//  Created by MAC on 10/28/12.
//  Copyright (c) 2012 BKHN. All rights reserved.
//

#import "ViewLoanViewController.h"

@implementation ViewLoanViewController
@synthesize currentLoan;
@synthesize borrowerLabel;
@synthesize targetLabel;
@synthesize completedLabel;
@synthesize historyTableView;
@synthesize loanNameLabel;

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addLoanDebtHistory_onSuccess : ) name:@"addLoanDebtHistory_onSuccess" object:nil];
    
    folyAppDelegate *delegate = [[UIApplication sharedApplication]delegate];
    
    if (!(currentLoan.l_finished == FALSE)) {
        payButton.hidden = YES;
    }

    self.loanNameLabel.text = currentLoan.l_name;
    self.borrowerLabel.text   = currentLoan.l_borrower;
    self.targetLabel.text   = [NSString stringWithFormat:@"%@", [currentLoan.l_amount stringValue]];
    
    historyArray = [NSMutableArray arrayWithArray:[DataBaseAccess getAllLoanHistoryInLoan:currentLoan]];
    NSLog(@"History count: %d", [historyArray count]);
    double completed = 0;
    for (LoanHistory *item in historyArray) {
        completed += [item.lh_amount doubleValue];
    }
    
    self.completedLabel.text = [NSString stringWithFormat:@"%0.0f", completed];
    [historyTableView reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) addLoanDebtHistory_onSuccess : (NSNotification*) notification {
    historyArray = [NSMutableArray arrayWithArray:[DataBaseAccess getAllLoanHistoryInLoan:currentLoan]];
    NSLog(@"History count: %d", [historyArray count]);
    double completed = 0;
    for (LoanHistory *item in historyArray) {
        completed += [item.lh_amount doubleValue];
    }
    
    self.completedLabel.text = [NSString stringWithFormat:@"%0.0f", completed];
    [historyTableView reloadData];
    
    if (!(currentLoan.l_finished == FALSE)) {
        payButton.hidden = YES;
    }
}

- (IBAction)backButtonPressed:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)payButtonPressed:(id)sender {
    payHistoryAlertView = [[PayHistoryAlertView alloc]initWithNibName:@"PayHistoryAlertView" bundle:nil];
    
    payHistoryAlertView.payType = 1;
    payHistoryAlertView.currentLoan = currentLoan;
    [payHistoryAlertView.addButton setTitle:@"Add to Loan History" forState:UIControlStateNormal];
    
    [self.view addSubview:payHistoryAlertView.view];
    NSLog(@"%@", [self.view subviews]);
}

#pragma mark - Table View Delegate
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return  1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [historyArray count];
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellInditifier = @"CellInditifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInditifier] ;
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellInditifier];
    }
    
    LoanHistory *row = (LoanHistory*)[historyArray objectAtIndex:indexPath.row];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"dd/MM/yyyy - HH:mm:ss"];
    
    cell.textLabel.text =       [row.lh_amount stringValue];
    cell.detailTextLabel.text = [dateFormatter stringFromDate:row.lh_date];
    return cell;
}

@end
